// pages/orders/orders.js
import api from '../../Sendingmethod/Sendingmethod'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //底部导航栏 index
        tabarindex: 'pages/orders/orders',
        statusBar_H: '',
        //弹出窗判断
        showon: false,
        currentDate: new Date().getTime(),
        minDate: new Date(2020, 0, 1),
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            } else if (type === 'month') {
                return `${value}月`;
            }
            return value;
        },
        //日期
        datetime: '',
        textbox: [{
                text: '今日订单',
                value: "9999.99"
            },
            {
                text: '今日金额',
                value: "999.99"
            },
            {
                text: '订单总数',
                value: "999999"
            },
            {
                text: '金额总数',
                value: "9999.99"
            },
        ],
        // 订单状态
        ordertype: [{
                name: '全部',
                type: ""
            },
            {
                name: '已支付',
                type: 3
            },
            {
                name: '已取消',
                type: 4
            },
            {
                name: '已退款',
                type: 5
            }
        ],
        // 订单index
        ordertype_index: "",
        /** */
        dataarr: [],
        totaldata: {},
        orderlist: [],
        lodingblen: false,
        pagenum: 1,
        triggered: true,
        //数据列表展开
        biaogeblen: false,
        // 开始时间
        starttime: '',
        // 开始时间
        endtime: '',
        // 设备id
        emid: '',
        datalist: {
            totalCount: '订单总数',
            refundCount: "退款订单",
            cancelCount: "取消订单",
            totalMoney: "订单金额",
            refundMoney: "退款金额",
            incomeMoney: "实收金额",
        },
        orderdata: {}
    },
    /**
     * 日期处理函数
     */
    datachuli(datebox) {
        const timestamp = datebox;
        const date = new Date(timestamp);
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始，需要加1
        const day = String(date.getDate()).padStart(2, '0');
        const formattedDate = `${year}-${month}-${day}`;
        console.log(formattedDate);
        return formattedDate
    },
    /**
     * 
     * 选择
     */
    ordertype_select(e) {
        console.log(e);
        if (this.data.ordertype_index !== e.target.dataset.index) {
            this.setData({
                ordertype_index: e.target.dataset.index
            })
        }
        this.getorder()
    },
    /**
     * 
     * 跳转
     */
    goto(e) {
        console.log(e.currentTarget.dataset.item);
        let data = JSON.stringify(e.currentTarget.dataset.item)
        wx.navigateTo({
            url: `/pages/Order_Details/Order_Details?ordata=${data}`,
        })

    },
    /**
     * 顶部表格展开
     */
    zhankaibiaoge() {
        this.setData({
            biaogeblen: !this.data.biaogeblen
        })
    },
    /**
     * 
     * 触底事件
     */
    Bottomingout() {
        console.log('触底');
        this.data.pagenum = this.data.pagenum + 1
        this.getorder(1)
        this.setData({
            lodingblen: true
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options, '首页传入数据');
        // console.log(options, '首页传入数据');
        if (options.emid) {
            this.setData({
                emid: options.emid
            })
        }


        //获取状态栏高度
        const statusBarHeight = wx.getWindowInfo().statusBarHeight;
        //获取胶囊属性
        const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
        //计算导航栏属性
        const navBarHeight = menuButtonInfo.height + (menuButtonInfo.top - statusBarHeight) * 2
        //赋值给 高度 变量
        this.setData({
            statusBar_H: (statusBarHeight + navBarHeight) * 2,
            menu_Info: statusBarHeight * 2
        })
        console.log(statusBarHeight, navBarHeight, this.statusBar_H, '页面刷新');
    },
    onInput(event) {
        console.log(event);
        this.setData({
            currentDate: event.detail
        });
    },
    /**
     * 打开弹出层
     */
    openprop() {
        this.setData({
            showon: true
        })
    },


    confirmfun() {
        console.log(1);
        this.setData({
            showon: false
        })
    },
    cancelfun() {
        console.log(2);
        let _that = this
        this.setData({
            showon: false,
            datetime: _that.datachuli(_that.data.currentDate)
        })
        console.log(this.data.datetime);

    },
    /**
     * 订单参数
     */
    async gettotal() {
        try {
            let res = await api.emorderTotal({
                startTime: this.data.starttime,
                endTime: this.data.endtime,
                device_id: this.data.emid
            })
            console.log(res, '全部数据');
            if (res.code == 0) {
                let newdata = {}

                for (const key in this.data.datalist) {
                    console.log(key, '当前key');
                    for (const key1 in res.data) {
                        // console.log(key1,'循环key');
                        if (key == key1) {
                            newdata[key] = res.data[key]

                        }

                    }
                }
                this.setData({
                    orderdata: newdata
                })
            } else {

            }
        } catch (error) {

        }

    },
    // 黑色
    addshop(e) {
        console.log(e.detail, '子组件传值');
        if (!e.detail.showblen) {
            console.log('走这里1');
            this.setData({
                showon: false
            })
        } else {
            console.log('走这里2');
            this.setData({
                showon: false,
                starttime: e.detail.datestr.starttime,
                endtime: e.detail.datestr.endtime,
                pageindex: 1
            })
            this.getorder()
            this.gettotal()
        }

    },


    /**
     * 订单列表
     */
    async getorder(num) {
        try {
            let data
            if (num == 1) {
                data = {
                    page: this.data.pagenum,
                    status: this.data.ordertype_index,
                    startTime: this.data.starttime,
                    endTime: this.data.endtime
                }
            } else {
                data = {
                    page: 1,
                    status: this.data.ordertype_index,
                    startTime: this.data.starttime,
                    endTime: this.data.endtime
                }
            }
            console.log(data, '传输数据');
            let res = await api.emorderList({
                ...data,
                device_id: this.data.emid
            })
            console.log(res, '全部列表');
            if (res.code == 0) {
                if (num == 1) {
                    this.setData({
                        orderlist: [...this.data.orderlist, ...res.data.list]
                    })
                } else {
                    this.setData({
                        orderlist: res.data.list
                    })
                }
            } else {

            }
            setTimeout(() => {
                this.setData({
                    lodingblen: false
                })
            }, 500);
        } catch (error) {
            console.error(error);
        }
    },

    /**
     * xialshuax 下拉刷新
     */
    xialshuax() {
        wx.showLoading({
            mask: true
        })
        console.log('下拉刷新触发');
        this.gettotal()
        this.getorder()
        setTimeout(() => {
            wx.hideLoading()
            this.setData({
                triggered: false
            })
        }, 1000)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let date = new Date().getTime()
        this.setData({
            starttime: this.datachuli(date),
            endtime: this.datachuli(date)
        })
        console.log('onshow');
        this.getorder()
        this.gettotal()

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})